﻿using SigowayCommonLib;
using SigowayCommonLib.ADO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.BL;
using WarehouseManager.Common;
using WarehouseManager.View;

namespace WarehouseManager
{
    /// <summary>
    ///  药品信息设置的Form
    /// </summary>
    public partial class DrupRegisterForm : Form
    {
        #region 变量
        /// <summary>
        /// 操作员ID
        /// </summary>
        private string strOperatorID = string.Empty;

        /// <summary>
        ///  操作员
        /// </summary>
        private string strOperator = string.Empty;

        /// <summary>
        ///  操作日期
        /// </summary>
        private string strOperatDate = string.Empty;

        /// <summary>
        ///  药品ID的OldText
        /// </summary>
        private string strOldDrugID = string.Empty;

        /// <summary>
        ///  药品信息设置BL
        /// </summary>
        private DrupRegisterBL blDrupRegister = null;

        /// <summary>
        ///  存在Flg
        /// </summary>
        private bool bExistFlg = false;

        private DBManager dbManager;
        #endregion

        public DrupRegisterForm(string _operatorID,string _operator)
        {
            InitializeComponent();
            //操作员
            this.strOperatorID = _operatorID;
            //操作员
            this.strOperator = _operator;
            this.blDrupRegister = new BL.DrupRegisterBL();
            this.dbManager = DBManager.GetDBManager();
        }

        #region DrupRegisterForm_Load
        /// <summary>
        ///  画面Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrupRegisterForm_Load(object sender, EventArgs e)
        {
            try
            {
                //初期化
                this.Init();
                this.txtDrugID.Focus();
            }
            catch (Exception ex)
            {
                MessageBoxHelp.ShowMsgError("初期启动失败!");
                LogManager.WriteLine("初期启动失败!Exception={0}", ex);
            }
        } 
        #endregion

        #region Init
        /// <summary>
        ///  初期化
        /// </summary>
        private void Init()
        {
            //画面初期化
            this.InitGUI();
            //事件定义
            this.InitEvent();
        } 
        #endregion

        #region InitGUI
        /// <summary>
        /// 画面初期化
        /// </summary>
        private void InitGUI()
        {
            //画面Data初期化
            this.InitData();
            this.ClearData(true);
        }
        #endregion

        #region InitEvent
        /// <summary>
        /// 事件定义
        /// </summary>
        private void InitEvent()
        {
            //药品ID
            this.txtDrugID.Validating += TxtDrugID_Validating;
            this.txtDrugID.Validated += TxtDrugID_Validated;

            ////药品名称
            //this.txtDrugName.Validating += TxtControl_Validating;
            ////货号
            //this.txtArtNo.Validating += TxtControl_Validating;
            ////单位
            //this.txtUnit.Validating += TxtControl_Validating;
            ////规格
            //this.txtDrugSpec.Validating += TxtControl_Validating;
            ////产地
            //this.txtProduceArea.Validating += TxtControl_Validating;


            //保存
            this.btnSave.Click += BtnSave_Click;
        }        
        #endregion

        #region InitData
        /// <summary>
        ///  画面Data初期化
        /// </summary>
        private void InitData()
        {
            //操作员
            this.lblOperator.Text = this.strOperator;
            //操作日期
            this.lblOperatDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
        #endregion

        #region TxtControl_Validating
        /// <summary>
        ///  Validating
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtControl_Validating(object sender, CancelEventArgs e)
        {
            TextBox txtBox = sender as TextBox;
            if (txtBox == null)
            {
                return;
            }
            //有值的时候
            if (!string.IsNullOrEmpty(txtBox.Text))
            {
                this.errorProvider.Clear();
                return;
            }
            switch (txtBox.Name)
            {
                //药品名称
                case "txtDrugName":
                    this.errorProvider.SetError(txtBox, "请输入药品名称");
                    break;
                //货号
                case "txtArtNo":
                    this.errorProvider.SetError(txtBox, "请输入货号");
                    break;
                //单位
                case "txtUnit":
                    this.errorProvider.SetError(txtBox, "请输入单位");
                    break;
                //规格
                case "txtDrugSpec":
                    this.errorProvider.SetError(txtBox, "请输入规格");
                    break;
                //产地
                case "txtProduceArea":
                    this.errorProvider.SetError(txtBox, "请输入产地");
                    break;
                default:
                    break;
            }
        } 
        #endregion

        #region TxtDrugID_Validated
        /// <summary>
        ///  药品ID的Validated
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtDrugID_Validated(object sender, EventArgs e)
        {
            this.strOldDrugID = this.txtDrugID.Text.Trim();
        }
        #endregion

        #region TxtDrugID_Validating
        /// <summary>
        ///  药品ID的Validating
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtDrugID_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                this.errorProvider.Clear();

                //药品ID
                string strDrugIDTemp = this.txtDrugID.Text.Trim();

                //药品ID为空的时候
                if (string.IsNullOrEmpty(strDrugIDTemp))
                {
                    //画面Data清空
                    this.ClearData(true);
                    //this.errorProvider.SetError(this.txtDrugID, "请输入编号");
                    return;
                }

                //药品ID没有变化的时候
                if (this.strOldDrugID == strDrugIDTemp)
                {
                    return;
                }

                //根据药品ID，从表DrupInfo药品信息取得
                var sqlText = string.Format("SELECT * FROM DrugInfo WHERE {0} = ?", ConstInfo.COL_DRUGID);
                IDictionary<string, object> dicParameter = new Dictionary<string, object>();
                dicParameter.Add(ConstInfo.COL_DRUGID, this.txtDrugID.Text.Trim());
                DataTable datInfo = dbManager.ExecuteQuery(new SQLObject(sqlText, dicParameter));

                if (datInfo == null || datInfo.Rows.Count == 0)
                {
                    this.bExistFlg = false;
                    this.ClearData(false);
                }
                else
                {
                    this.bExistFlg = true;
                    //画面的信息设定
                    this.SetData(datInfo);
                }
            }
            catch (Exception ex)
            {
                e.Cancel = true;
                this.txtDrugID.Text = this.strOldDrugID;
                MessageBoxHelp.ShowMsgError("药品信息检索失败");
                LogManager.WriteLine("药品信息检索失败!Exception={0}", ex);
            }
        } 
        #endregion

        #region BtnSave_Click
        /// <summary>
        ///  保存的Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.errorProvider.Clear();
                //事前check
                if (!this.BeforCheck())
                {
                    return;
                }

                //保存
                this.Save();

                MessageBoxHelp.ShowMsgInfor("药品信息保存成功!");
                //画面数据清空
                this.ClearData(true);
                this.txtDrugID.Focus();
                this.txtDrugID.SelectAll();
            }
            catch (Exception ex)
            {
                MessageBoxHelp.ShowMsgError("药品信息保存失败!");
                LogManager.WriteLine("药品信息保存失败!Exception={0}", ex);
            }
        } 
        #endregion

        #region Save
        /// <summary>
        ///  保存
        /// </summary>
        private void Save()
        {
            string strDateTime = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
            //插入的时候
            if (!this.bExistFlg)
            {
                //数据插入
                var sqlText = "INSERT INTO DrugInfo VALUES(?,?,?,?,?,?,?,?,?,?)";

                IDictionary<string, object> dicParameter = new Dictionary<string, object>();
                dicParameter.Add(ConstInfo.COL_DRUGID, this.txtDrugID.Text.Trim());
                dicParameter.Add(ConstInfo.COL_DRUGNAME, this.txtDrugName.Text);
                dicParameter.Add(ConstInfo.COL_ARTNO, this.txtArtNo.Text);
                dicParameter.Add(ConstInfo.COL_UNIT, this.txtUnit.Text);
                dicParameter.Add(ConstInfo.COL_DRUGSPEC, this.txtDrugSpec.Text);
                dicParameter.Add(ConstInfo.COL_PRODUCEAREA, this.txtProduceArea.Text);
                dicParameter.Add(ConstInfo.COL_CREATEUSER, this.strOperatorID);
                dicParameter.Add(ConstInfo.COL_CREATEDATE, strDateTime);
                dicParameter.Add(ConstInfo.COL_UPDATEUSER, "");
                dicParameter.Add(ConstInfo.COL_UPDATEDATE, "");
                this.dbManager.ExecuteNonQueryAsync(new SQLObject(sqlText, dicParameter));
            }
            else
            {
                var sqlText = string.Format(@"UPDATE DrugInfo SET 
{0} = ? ,
{1} = ? ,
{2} = ? ,
{3} = ? ,
{4} = ? ,
{5} = ? ,
{6} = ?
WHERE {7} = ?", ConstInfo.COL_DRUGNAME, ConstInfo.COL_ARTNO, ConstInfo.COL_UNIT, ConstInfo.COL_DRUGSPEC,
  ConstInfo.COL_PRODUCEAREA, ConstInfo.COL_UPDATEUSER, ConstInfo.COL_UPDATEDATE,
  ConstInfo.COL_DRUGID);

                IDictionary<string, object> dicParameter = new Dictionary<string, object>();
                dicParameter.Add(ConstInfo.COL_DRUGNAME, this.txtDrugName.Text);
                dicParameter.Add(ConstInfo.COL_ARTNO, this.txtArtNo.Text);
                dicParameter.Add(ConstInfo.COL_UNIT, this.txtUnit.Text);
                dicParameter.Add(ConstInfo.COL_DRUGSPEC, this.txtDrugSpec.Text);
                dicParameter.Add(ConstInfo.COL_PRODUCEAREA, this.txtProduceArea.Text);
                dicParameter.Add(ConstInfo.COL_UPDATEUSER, this.strOperatorID);
                dicParameter.Add(ConstInfo.COL_UPDATEDATE, strDateTime);
                dicParameter.Add(ConstInfo.COL_DRUGID, this.txtDrugID.Text.Trim());
                this.dbManager.ExecuteNonQueryAsync(new SQLObject(sqlText, dicParameter));
            }
        } 
        #endregion

        #region BeforCheck
        /// <summary>
        ///  保存钱的检查
        /// </summary>
        /// <returns>true:检查通过 False:检查失败</returns>
        private bool BeforCheck()
        {
            //药品ID
            if (string.IsNullOrEmpty(this.txtDrugID.Text.Trim()))
            {
                this.errorProvider.SetError(this.txtDrugID, "请输入编号");
                this.txtDrugID.Focus();
                return false;
            }

            //药品名称
            if (string.IsNullOrEmpty(this.txtDrugName.Text))
            {
                this.errorProvider.SetError(this.txtDrugName, "请输入药品名称");
                this.txtDrugName.Focus();
                return false;
            }

            //货号
            if (string.IsNullOrEmpty(this.txtArtNo.Text))
            {
                this.errorProvider.SetError(this.txtArtNo, "请输入货号");
                this.txtArtNo.Focus();
                return false;
            }

            //单位
            if (string.IsNullOrEmpty(this.txtUnit.Text.Trim()))
            {
                this.errorProvider.SetError(this.txtUnit, "请输入单位");
                this.txtUnit.Focus();
                return false;
            }

            //规格
            if (string.IsNullOrEmpty(this.txtDrugSpec.Text))
            {
                this.errorProvider.SetError(this.txtDrugSpec, "请输入规格");
                this.txtDrugSpec.Focus();
                return false;
            }

            //产地
            if (string.IsNullOrEmpty(this.txtProduceArea.Text))
            {
                this.errorProvider.SetError(this.txtProduceArea, "请输入产地");
                this.txtProduceArea.Focus();
                return false;
            }

            return true;
        } 
        #endregion

        #region ClearData
        /// <summary>
        ///  画面数据清空
        /// </summary>
        private void ClearData(bool bClearDrugIDFlg)
        {
            if (bClearDrugIDFlg)
            {
                //药品ID
                this.txtDrugID.Text = string.Empty;
                this.strOldDrugID = string.Empty;
            }
                      
            //药品名称
            this.txtDrugName.Text = string.Empty;
            //货号
            this.txtArtNo.Text = string.Empty;
            //单位
            this.txtUnit.Text = string.Empty;
            //规格
            this.txtDrugSpec.Text = string.Empty;
            //产地
            this.txtProduceArea.Text = string.Empty;
        }
        #endregion

        #region SetData
        /// <summary>
        ///  画面的Data设定
        /// </summary>
        /// <param name="datInfo"></param>
        private void SetData(DataTable datInfo)
        {
            //数据没有的时候
            if (datInfo == null || datInfo.Rows.Count == 0)
            {
                return;
            }

            DataRow rowInfo = datInfo.Rows[0];
            //药品名称
            this.txtDrugName.Text = rowInfo[ConstInfo.COL_DRUGNAME].ToString();
            //货号
            this.txtArtNo.Text = rowInfo[ConstInfo.COL_ARTNO].ToString();
            //单位
            this.txtUnit.Text = rowInfo[ConstInfo.COL_UNIT].ToString();
            //规格
            this.txtDrugSpec.Text = rowInfo[ConstInfo.COL_DRUGSPEC].ToString();
            //产地
            this.txtProduceArea.Text = rowInfo[ConstInfo.COL_PRODUCEAREA].ToString();
        } 
        #endregion

        private void btnSearchDurgInfo_Click(object sender, EventArgs e)
        {
            DrugListForm formDrugList = new DrugListForm(this.strOperator);
            formDrugList.ShowDialog();
        }

    }
}
